package com.example.yunhailutong.zhongtan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.ActivityFogetPwd;
import com.example.yunhailutong.zhongtan.activity.ActivityLogin;
import com.example.yunhailutong.zhongtan.activity.ActivityUpdateUserMessage;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.UserInfoEntity;
import com.example.yunhailutong.zhongtan.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {


    Unbinder unbinder;
    RelativeLayout changeOld;
    @BindView(R.id.user_info)
    ImageView userInfo;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.ziliao)
    LinearLayout ziliao;

    @BindView(R.id.mima)
    LinearLayout mima;
    @BindView(R.id.tuichu)
    LinearLayout tuichu;

    private View view;

    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        // Inflate the layout for this fragment
        Map<String, String> params = new HashMap<>();
        params.put("id", App.id);
        params.put("token", App.token);
        //new PersionBiz().GetUserInfo(params);
        initView();
        ziliao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(ActivityUpdateUserMessage.class);
            }
        });
        mima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(ActivityFogetPwd.class);
            }
        });
        tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });
        return view;
    }

    private void initView() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    @Subscribe
    public void log(OldsEvent.GetUserInfoEvent msg) {
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            UserInfoEntity entity = (UserInfoEntity) msg.obj;
            userName.setText(entity.getData().getUserInfo().getName());
        }
    }
    public void logout() {
        App.isLogin = false;
        App.number = "12312312312313";
        App.oldPhoneNumber = "";
        SharedPreferencesUtils.setParam(getActivity(), "islogin", false);
        SharedPreferencesUtils.setParam(getActivity(), "number", "");
        SharedPreferencesUtils.setParam(getActivity(), "oldNumber", "");
        App.initpush();
        Intent intent = new Intent(getActivity(),ActivityLogin.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {

    }
}
